<?php


namespace App\Http\Controllers\Admin\Connect;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

//人脉奖池
class PrizePoolController extends BaseController
{
    /**
     * @api {post} /connect/prize-pool 添加人脉奖池
     *
     * @apiGroup 人脉-奖池
     *
     * @apiParam {String} prize_pool_name 奖池名称，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function add(Request $request)
    {
        $param = $request->only('prize_pool_name');
        $v = app('validator')->make($param, [
            'prize_pool_name' => 'required|string|max:16',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_connect_prize_pool')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /connect/prize-pool 编辑人脉奖池
     *
     * @apiGroup 人脉-奖池
     *
     * @apiParam {Int} id 奖池id，必须
     * @apiParam {String} prize_pool_name 奖池名称，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'prize_pool_name');
        $v = app('validator')->make($param, [
            'id' => 'required|integer|min:1',
            'prize_pool_name' => 'required|string|max:16',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_connect_prize_pool')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {delete} /connect/prize-pool 删除人脉奖池
     *
     * @apiGroup 人脉-奖池
     *
     * @apiParam {Int} id 奖池id，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $id = $request->id;

        if (empty($id)) throw new BasicException(0, '请选择要删除的记录');

        $pool = app('model_connect_prize_pool')->where('id', $id)->first();
        if (empty($pool)) throw new BasicException(0, 'id错误，奖池不存在');

        $res = app('model_connect_prize_pool')->where('id', $id)->update(['is_del' => 1]);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '删除失败');
    }

    /**
     * @api {get} /connect/prize-pool/all 获取所有人脉奖池
     *
     * @apiGroup 人脉-奖池
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 1,
    "name": "奖池一"
    },
    {
    "id": 2,
    "name": "奖池二"
    },
    {
    "id": 3,
    "name": "奖池三"
    }
    ]
    }
     */
    public function all()
    {
        $list = app('model_connect_prize_pool')->select('id', 'name')->where('is_del', 0)->get();
        return Response::sendData($list);
    }
}